﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class provides methods to handle mathematical quaternions.
    /// </summary>
    [Serializable]
    public class Quaternion : IQuaternion
    {
        /// <summary>
        /// The real number path of the quaternion.
        /// </summary>
        private double x0;

        /// <summary>
        /// The imaginary number path i of the quaternion.
        /// </summary>
        private double i;

        /// <summary>
        /// The imaginary number path j of the quaternion.
        /// </summary>
        private double j;

        /// <summary>
        /// The imaginary number path k of the quaternion.
        /// </summary>
        private double k;

        /// <summary>
        /// Initializes a new instance of the <see cref="Quaternion"/> class.
        /// </summary>
        public Quaternion()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Quaternion"/> class.
        /// </summary>
        /// <param name="x0">The real number part x0 of the quaternion.</param>
        public Quaternion(double x0)
        {
            this.x0 = x0;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Quaternion"/> class.
        /// </summary>
        /// <param name="quaternion">Another quaternion that should be cloned.</param>
        public Quaternion(Quaternion quaternion)
        {
            if (quaternion == (Quaternion) null)
            {
                throw new ArgumentNullException("quaternion");
            }

            this.x0 = quaternion.X0;
            this.i = quaternion.I;
            this.j = quaternion.J;
            this.k = quaternion.K;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Quaternion"/> class.
        /// </summary>
        /// <param name="x0">The real number part x0 of the quaternion.</param>
        /// <param name="x1">The imaginary number path i of the quaternion.</param>
        /// <param name="x2">The imaginary number path j of the quaternion.</param>
        /// <param name="x3">The imaginary number path k of the quaternion.</param>
        public Quaternion(double x0, double x1, double x2, double x3)
        {
            this.x0 = x0;
            this.i = x1;
            this.j = x2;
            this.k = x3;
        }

        /// <summary>
        /// Gets or sets the real number part of the quaternion.
        /// </summary>
        /// <value>The real number part of the quaternion.</value>
        public double X0
        {
            get { return x0; }
            set { x0 = value; }
        }

        /// <summary>
        /// Gets or sets the imaginary number part i of the quaternion.
        /// </summary>
        /// <value>The imaginary number part i of the quaternion.</value>
        public double I
        {
            get { return i; }
            set { i = value; }
        }

        /// <summary>
        /// Gets or sets the imaginary number part j of the quaternion.
        /// </summary>
        /// <value>The imaginary number part j of the quaternion.</value>
        public double J
        {
            get { return j; }
            set { j = value; }
        }

        /// <summary>
        /// Gets or sets the imaginary number part k of the quaternion.
        /// </summary>
        /// <value>The imaginary number part k of the quaternion.</value>
        public double K
        {
            get { return k; }
            set { k = value; }
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first quaternion.</param>
        /// <param name="b">The second quaternion.</param>
        /// <returns>The result of the operator.</returns>
        public static Quaternion operator +(Quaternion a, Quaternion b)
        {
            if (a == (Quaternion) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Quaternion) null)
            {
                throw new ArgumentNullException("b");
            }

            Quaternion result = new Quaternion();

            result.X0 = a.X0 + b.X0;
            result.I = a.I + b.I;
            result.J = a.J + b.J;
            result.K = a.K + b.K;

            return result;
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first quaternion.</param>
        /// <param name="b">The second quaternion.</param>
        /// <returns>The result of the operator.</returns>
        public static Quaternion operator -(Quaternion a, Quaternion b)
        {
            if (a == (Quaternion) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Quaternion) null)
            {
                throw new ArgumentNullException("b");
            }

            Quaternion result = new Quaternion();

            result.X0 = a.X0 - b.X0;
            result.I = a.I - b.I;
            result.J = a.J - b.J;
            result.K = a.K - b.K;

            return result;
        }

        /// <summary>
        /// Implements the operator *.
        /// </summary>
        /// <param name="a">The first quaternion.</param>
        /// <param name="b">The second quaternion.</param>
        /// <returns>The result of the operator.</returns>
        public static Quaternion operator *(Quaternion a, Quaternion b)
        {
            if (a == (Quaternion) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Quaternion) null)
            {
                throw new ArgumentNullException("b");
            }

            Quaternion result = new Quaternion();

            result.X0 = a.X0 * b.X0 - a.I * b.I - a.J * b.J - a.K * b.K;
            result.I = a.X0 * b.I + a.I * b.X0 + a.J * b.K - a.K * b.J;
            result.J = a.X0 * b.J - a.I * b.K + a.J * b.X0 + a.K * b.I;
            result.K = a.X0 * b.K + a.I * b.J - a.J * b.I + a.K * b.X0;

            return result;
        }

        /// <summary>
        /// Implements the operator /.
        /// </summary>
        /// <param name="a">The first quaternion.</param>
        /// <param name="b">The second quaternion.</param>
        /// <returns>The result of the operator.</returns>
        public static Quaternion operator /(Quaternion a, Quaternion b)
        {
            return a * b.Inverse;
        }

        /// <summary>
        /// Gets the conjugation of the quaternion.
        /// </summary>
        /// <returns>The conjugation of the quaternion.</returns>
        public Quaternion Conjugation
        {
            get
            {
                Quaternion result = new Quaternion(this);

                result.I = -result.I;
                result.J = -result.J;
                result.K = -result.K;

                return result;
            }
        }

        /// <summary>
        /// Normalizes the current instance of the quaternion.
        /// </summary>
        /// <returns>The normalized instance of the quaternion.</returns>
        public Quaternion Normalize()
        {
            if (this.Length == 0)
            {
                throw new IllegalArithmeticException("Division by zero not allowed.");
            }

            Quaternion q = new Quaternion();
            double length = this.Length;

            q.X0 = this.x0 / length;
            q.I = this.i / length;
            q.J = this.j / length;
            q.K = this.k / length;

            return q;
        }

        /// <summary>
        /// Gets the multiplicative inverse of the current quaternion.
        /// </summary>
        /// <returns>The multiplicative inverse of the current quaternion.</returns>
        public Quaternion Inverse
        {
            get
            {
                if (this.Length == 0)
                {
                    throw new IllegalArithmeticException("Division by zero not allowed.");
                }

                Quaternion q = this;
                double length = this.Length;

                q.x0 /= Math.Pow(length, 2);
                q.i /= Math.Pow(length, 2);
                q.j /= Math.Pow(length, 2);
                q.k /= Math.Pow(length, 2);

                return q;
            }
        }

        /// <summary>
        /// Gets the length of the quaternion.
        /// </summary>
        /// <returns>The length of the quaternion.</returns>
        public double Length
        {
            get { return Math.Sqrt(Math.Pow(this.x0, 2) + Math.Pow(this.i, 2) + Math.Pow(this.j, 2) + Math.Pow(this.k, 2)); }
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first quaternion.</param>
        /// <param name="b">The second quaternion.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Quaternion a, Quaternion b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.X0 == b.X0) && (a.I == b.I) && (a.J == b.J) && (a.K == b.K));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first quaternion.</param>
        /// <param name="b">The second quaternion.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Quaternion a, Quaternion b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Compares the current quaternioninstance to another quaternion.
        /// </summary>
        /// <param name="compare">The quaternion to compare.</param>
        /// <returns>True if the current quaternioninstance is even to the specified quaternion otherwise, false.</returns>
        public bool CompareTo(Quaternion compare)
        {
            return this == compare;
        }

        /// <summary>
        /// Copies this current instance.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public Quaternion Copy()
        {
            return new Quaternion(this.x0, this.i, this.j, this.k);
        }

        /// <summary>
        /// Determines whether the real number part is zero.
        /// </summary>
        /// <returns>
        /// True if real number part is zero otherwise, false.
        /// </returns>
        public bool IsRealNumberPartZero
        {
            get { return this.x0 == 0; }
        }

        /// <summary>
        /// Determines whether the imaginary number parts i, j and k are zero.
        /// </summary>
        /// <returns>
        /// True if imaginary number parts i, j and k are zero otherwise, false.
        /// </returns>
        public bool IsImaginaryNumberPartZero
        {
            get { return ((this.i == 0) && (this.j == 0) && (this.k == 0)); }
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.x0.ToString() + " + " + this.i.ToString() + "i + " + this.j.ToString() + "j + " +
                   this.k.ToString() + "k";
        }
    }
}